package com.wengu.rabbitmq.direct;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.wengu.rabbitmq.util.ConnectionUtil;
import org.springframework.amqp.core.ExchangeTypes;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author LoserJohn
 * @create 2020-09-07 14:40
 */
//定向direct模型的生产者
//direct交换机 会将消息发送给指定的绑定的队列
//只能由队列来存储消息，交换机接受到数据时如果找不到绑定的队列，数据则会丢失(交换机不存数据)
//必须先执行一下，为了创建交换机，消费者然后才可以通过队列绑定交换机
public class Send {
    static final String EXCHANGE_NAME = "exchange.direct";
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.获取连接
        Connection connection = ConnectionUtil.getConnection();
        //2.创建通道
        Channel channel = connection.createChannel();
        //3.声明交换机
        //参数1:交换机名称,参数2:交换机类型
        channel.exchangeDeclare(EXCHANGE_NAME, ExchangeTypes.DIRECT);
        //4.发送消息到交换机中
        //案例：商品CRUD   routingkey: goods.insert , goods.update , goods.query , goods.delete
        //消费者绑定交换机时指定关心的数据的routingkey，交换机会将消息发送给绑定了消息routingkey的队列
        String message = "goods.query";
        channel.basicPublish(EXCHANGE_NAME,"goods.query",null,message.getBytes());
        System.out.println("send message : "+ message);
        //5.关闭连接
        channel.close();
        connection.close();
    }
}
